package indi.jp.project.business.jingying.service.impl;

import indi.jp.project.business.jingying.dao.ActivityPlanDAO;
import indi.jp.project.business.jingying.pojo.po.ActivityPlanPO;
import indi.jp.project.business.jingying.pojo.query.ActivityPlanQuery;
import indi.jp.project.business.jingying.service.ActivityPlanService;
import indi.jp.project.core.entity.Datagrid;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.util.SerialUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 实现
 *
 * @author jiangping
 * @date 2022/5/10
 */
@Service
public class ActivityPlanServiceImpl implements ActivityPlanService {

    @Resource
    private ActivityPlanDAO activityPlanDAO;

    @Resource
    private SerialUtil serialUtil;

    @Override
    public JsonResult queryGrid(ActivityPlanQuery query) throws Exception {
        List<ActivityPlanPO> list = activityPlanDAO.queryList(query);
        int count = activityPlanDAO.queryCount(query);
        Datagrid<ActivityPlanPO> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }

    @Override
    public JsonResult save(ActivityPlanPO activityPlanPO) throws Exception {
        if (StringUtils.isBlank(activityPlanPO.getId())) {
            activityPlanPO.setId(String.valueOf(serialUtil.nextId()));
            activityPlanDAO.insert(activityPlanPO);
        } else {
            activityPlanDAO.update(activityPlanPO);
        }
        return JsonResult.success();
    }

    @Override
    public JsonResult findById(String id) throws Exception {
        ActivityPlanPO activityPlanPO = activityPlanDAO.findById(id);
        return JsonResult.success(activityPlanPO);
    }

    @Override
    public JsonResult deleteById(String id) throws Exception {
        activityPlanDAO.deleteById(id);
        return JsonResult.success();
    }
}
